<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\AdminUser\Action\Admin\Role;

use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Plugin\AdminUser\Middleware\AdminAuthMiddleware;
use MoChat\Plugin\AdminUser\Middleware\PermissionMiddleware;

/**
 * 权限.
 *
 * Class PermissionByUser.
 * @Controller
 */
class PermissionByUser extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var StdoutLoggerInterface
     */
    private $logger;

    /**
     * @RequestMapping(path="/admin/adminUser/role/permissionByUser", methods="get")
     * @Middlewares({
     *     @Middleware(AdminAuthMiddleware::class),
     *     @Middleware(PermissionMiddleware::class)
     * })
     * @return array 返回数组
     */
    public function handle(): array
    {
        // 用户信息
        $adminUser = user();
        // 账户是超级管理员-获取全部菜单
        if ($adminUser['isRoot'] === 1) {
            return [['dataPermission' => 2,
                'icon' => '',
                'id' => 2,
                'level' => 1,
                'linkType' => 1,
                'linkUrl' => '_baseSysManager',
                'name' => '平台管理',
                'isPageMenu' => 1,
                'children' => [[
                    'dataPermission' => 1,
                    'icon' => 'home',
                    'id' => 1,
                    'isPageMenu' => 1,
                    'level' => 2,
                    'linkType' => 1,
                    'linkUrl' => '',
                    'name' => '系统首页',
                    'children' => [[
                        'dataPermission' => 2,
                        'icon' => '',
                        'id' => 3,
                        'isPageMenu' => 1,
                        'level' => 3,
                        'linkType' => 1,
                        'linkUrl' => '/corpData/index',
                        'name' => '系统首页',
                        'children' => [[
                            'dataPermission' => 2,
                            'icon' => '',
                            'id' => 4,
                            'isPageMenu' => 1,
                            'level' => 4,
                            'linkType' => 1,
                            'linkUrl' => '/user/passwordReset',
                            'name' => '修改密码', ]],
                    ]],
                ],
                    [
                        'dataPermission' => 1,
                        'icon' => 'home',
                        'id' => 1,
                        'isPageMenu' => 1,
                        'level' => 2,
                        'linkType' => 1,
                        'linkUrl' => '',
                        'name' => '菜单管理',
                        'children' => [[
                            'dataPermission' => 2,
                            'icon' => '',
                            'id' => 3,
                            'isPageMenu' => 1,
                            'level' => 3,
                            'linkType' => 1,
                            'linkUrl' => '/menu/index',
                            'name' => '菜单管理',
                            'children' => [
                                [
                                    'dataPermission' => 2,
                                    'icon' => '',
                                    'id' => 4,
                                    'isPageMenu' => 1,
                                    'level' => 4,
                                    'linkType' => 1,
                                    'linkUrl' => '/menu/index@add',
                                    'name' => '添加(按钮)'
                                ]
                            ],
                        ]],
                    ],
                    [
                        'dataPermission' => 1,
                        'icon' => 'solution',
                        'id' => 2,
                        'isPageMenu' => 1,
                        'level' => 2,
                        'linkType' => 1,
                        'linkUrl' => '/tenant/index',
                        'name' => '租户管理',
                        'children' => [[
                            'dataPermission' => 2,
                            'icon' => '',
                            'id' => 3,
                            'isPageMenu' => 1,
                            'level' => 3,
                            'linkType' => 1,
                            'linkUrl' => '/tenant/index',
                            'name' => '租户列表',
                            'children' => [[
                                'dataPermission' => 2,
                                'icon' => '',
                                'id' => 4,
                                'isPageMenu' => 1,
                                'level' => 3,
                                'linkType' => 1,
                                'linkUrl' => '/tenant/create',
                                'name' => '添加租户',
                            ],
                                [
                                    'dataPermission' => 2,
                                    'icon' => '',
                                    'id' => 4,
                                    'isPageMenu' => 1,
                                    'level' => 3,
                                    'linkType' => 1,
                                    'linkUrl' => '/tenant/edit',
                                    'name' => '编辑租户',
                                ], ], ], ], ], [
                                    'dataPermission' => 1,
                                    'icon' => 'shop',
                                    'id' => 2,
                                    'isPageMenu' => 1,
                                    'level' => 2,
                                    'linkType' => 1,
                                    'linkUrl' => '/product/skuIndex',
                                    'name' => '套餐管理',
                                    'children' => [[
                                        'dataPermission' => 2,
                                        'icon' => '',
                                        'id' => 3,
                                        'isPageMenu' => 1,
                                        'level' => 3,
                                        'linkType' => 1,
                                        'linkUrl' => '/product/skuIndex',
                                        'name' => '套餐列表',
                                        'children' => [
                                            [
                                                'dataPermission' => 2,
                                                'icon' => '',
                                                'id' => 4,
                                                'isPageMenu' => 1,
                                                'level' => 4,
                                                'linkType' => 1,
                                                'linkUrl' => '/product/skuCreate',
                                                'name' => '新增套餐',
                                            ],
                                            [
                                                'dataPermission' => 2,
                                                'icon' => '',
                                                'id' => 4,
                                                'isPageMenu' => 1,
                                                'level' => 4,
                                                'linkType' => 1,
                                                'linkUrl' => '/product/skuEdit',
                                                'name' => '修改套餐',
                                            ], ],
                                    ],
                                    ], ], [
                                        'dataPermission' => 1,
                                        'icon' => 'solution',
                                        'id' => 2,
                                        'isPageMenu' => 1,
                                        'level' => 2,
                                        'linkType' => 1,
                                        'linkUrl' => '/agent/index',
                                        'name' => '代理管理',
                                        'children' => [[
                                            'dataPermission' => 2,
                                            'icon' => '',
                                            'id' => 3,
                                            'isPageMenu' => 1,
                                            'level' => 3,
                                            'linkType' => 1,
                                            'linkUrl' => '/agent/index',
                                            'name' => '代理列表',
                                            'children' => [[
                                                'dataPermission' => 2,
                                                'icon' => '',
                                                'id' => 4,
                                                'isPageMenu' => 1,
                                                'level' => 4,
                                                'linkType' => 1,
                                                'linkUrl' => '/agent/create',
                                                'name' => '添加代理', ], [
                                                    'dataPermission' => 2,
                                                    'icon' => '',
                                                    'id' => 4,
                                                    'isPageMenu' => 1,
                                                    'level' => 4,
                                                    'linkType' => 1,
                                                    'linkUrl' => '/agent/edit',
                                                    'name' => '修改代理', ]],
                                        ],
                                            [
                                                'dataPermission' => 2,
                                                'icon' => '',
                                                'id' => 4,
                                                'isPageMenu' => 1,
                                                'level' => 3,
                                                'linkType' => 1,
                                                'linkUrl' => '/agent/level',
                                                'name' => '代理等级',
                                            ], ], ], [
                                                'dataPermission' => 1,
                                                'icon' => 'reconciliation',
                                                'id' => 2,
                                                'isPageMenu' => 1,
                                                'level' => 2,
                                                'linkType' => 1,
                                                'linkUrl' => '/order/index',
                                                'name' => '财务管理',
                                                'children' => [[
                                                    'dataPermission' => 2,
                                                    'icon' => '',
                                                    'id' => 3,
                                                    'isPageMenu' => 1,
                                                    'level' => 3,
                                                    'linkType' => 1,
                                                    'linkUrl' => '/order/index',
                                                    'name' => '订单列表', ]],
                                            ], [
                                                'dataPermission' => 1,
                                                'icon' => 'setting',
                                                'id' => 2,
                                                'isPageMenu' => 1,
                                                'level' => 2,
                                                'linkType' => 1,
                                                'linkUrl' => '/system/oem',
                                                'name' => '系统管理',
                                                'children' => [[
                                                    'dataPermission' => 2,
                                                    'icon' => '',
                                                    'id' => 3,
                                                    'isPageMenu' => 1,
                                                    'level' => 3,
                                                    'linkType' => 1,
                                                    'linkUrl' => '/system/oem',
                                                    'name' => 'OEM信息配置', ],
                                                    [
                                                        'dataPermission' => 2,
                                                        'icon' => '',
                                                        'id' => 4,
                                                        'isPageMenu' => 1,
                                                        'level' => 3,
                                                        'linkType' => 1,
                                                        'linkUrl' => '/system/storage',
                                                        'name' => '存储OSS设置',
                                                    ], [
                                                        'dataPermission' => 2,
                                                        'icon' => '',
                                                        'id' => 3,
                                                        'isPageMenu' => 1,
                                                        'level' => 3,
                                                        'linkType' => 1,
                                                        'linkUrl' => '/system/wechat',
                                                        'name' => '微信接口配置', ],
                                                    [
                                                        'dataPermission' => 2,
                                                        'icon' => '',
                                                        'id' => 4,
                                                        'isPageMenu' => 1,
                                                        'level' => 3,
                                                        'linkType' => 1,
                                                        'linkUrl' => '/system/upgrade',
                                                        'name' => '系统版本更新',
                                                    ], ], ],
                ], ]];
        }
        // 账户是代理-获取权限菜单
        return [['dataPermission' => 2,
            'icon' => '',
            'id' => 2,
            'level' => 1,
            'linkType' => 1,
            'linkUrl' => '_baseSysManager',
            'name' => '平台管理',
            'isPageMenu' => 1,
            'children' => [[
                'dataPermission' => 1,
                'icon' => 'home',
                'id' => 1,
                'isPageMenu' => 1,
                'level' => 2,
                'linkType' => 1,
                'linkUrl' => '',
                'name' => '系统首页',
                'children' => [[
                    'dataPermission' => 2,
                    'icon' => '',
                    'id' => 3,
                    'isPageMenu' => 1,
                    'level' => 3,
                    'linkType' => 1,
                    'linkUrl' => '/corpData/index',
                    'name' => '系统首页',
                    'children' => [[
                        'dataPermission' => 2,
                        'icon' => '',
                        'id' => 4,
                        'isPageMenu' => 1,
                        'level' => 4,
                        'linkType' => 1,
                        'linkUrl' => '/user/passwordReset',
                        'name' => '修改密码', ]],
                ]],
            ],
                [
                    'dataPermission' => 1,
                    'icon' => 'solution',
                    'id' => 2,
                    'isPageMenu' => 1,
                    'level' => 2,
                    'linkType' => 1,
                    'linkUrl' => '/tenant/index',
                    'name' => '租户管理',
                    'children' => [[
                        'dataPermission' => 2,
                        'icon' => '',
                        'id' => 3,
                        'isPageMenu' => 1,
                        'level' => 3,
                        'linkType' => 1,
                        'linkUrl' => '/tenant/index',
                        'name' => '租户管理',
                        'children' => [[
                            'dataPermission' => 2,
                            'icon' => '',
                            'id' => 4,
                            'isPageMenu' => 1,
                            'level' => 3,
                            'linkType' => 1,
                            'linkUrl' => '/tenant/create',
                            'name' => '添加租户',
                        ],
                            [
                                'dataPermission' => 2,
                                'icon' => '',
                                'id' => 4,
                                'isPageMenu' => 1,
                                'level' => 3,
                                'linkType' => 1,
                                'linkUrl' => '/tenant/edit',
                                'name' => '编辑租户',
                            ], ], ], ], ], [
                                'dataPermission' => 1,
                                'icon' => 'shop',
                                'id' => 2,
                                'isPageMenu' => 1,
                                'level' => 2,
                                'linkType' => 1,
                                'linkUrl' => '/product/skuIndex',
                                'name' => '套餐管理',
                                'children' => [[
                                    'dataPermission' => 2,
                                    'icon' => '',
                                    'id' => 3,
                                    'isPageMenu' => 1,
                                    'level' => 3,
                                    'linkType' => 1,
                                    'linkUrl' => '/product/skuIndex',
                                    'name' => '套餐管理',
                                    'children' => [
                                        [
                                            'dataPermission' => 2,
                                            'icon' => '',
                                            'id' => 4,
                                            'isPageMenu' => 1,
                                            'level' => 4,
                                            'linkType' => 1,
                                            'linkUrl' => '/product/skuCreate',
                                            'name' => '新增套餐',
                                        ],
                                        [
                                            'dataPermission' => 2,
                                            'icon' => '',
                                            'id' => 4,
                                            'isPageMenu' => 1,
                                            'level' => 4,
                                            'linkType' => 1,
                                            'linkUrl' => '/product/skuEdit',
                                            'name' => '修改套餐',
                                        ], ],
                                ],
                                ], ], [
                                    'dataPermission' => 1,
                                    'icon' => 'solution',
                                    'id' => 2,
                                    'isPageMenu' => 1,
                                    'level' => 2,
                                    'linkType' => 1,
                                    'linkUrl' => '/agent/index',
                                    'name' => '代理管理',
                                    'children' => [[
                                        'dataPermission' => 2,
                                        'icon' => '',
                                        'id' => 3,
                                        'isPageMenu' => 1,
                                        'level' => 3,
                                        'linkType' => 1,
                                        'linkUrl' => '/agent/index',
                                        'name' => '代理管理',
                                        'children' => [[
                                            'dataPermission' => 2,
                                            'icon' => '',
                                            'id' => 4,
                                            'isPageMenu' => 1,
                                            'level' => 4,
                                            'linkType' => 1,
                                            'linkUrl' => '/agent/create',
                                            'name' => '添加代理', ], [
                                                'dataPermission' => 2,
                                                'icon' => '',
                                                'id' => 4,
                                                'isPageMenu' => 1,
                                                'level' => 4,
                                                'linkType' => 1,
                                                'linkUrl' => '/agent/edit',
                                                'name' => '修改代理', ]],
                                    ],
                                    ], ], [
                                        'dataPermission' => 1,
                                        'icon' => 'reconciliation',
                                        'id' => 2,
                                        'isPageMenu' => 1,
                                        'level' => 2,
                                        'linkType' => 1,
                                        'linkUrl' => '/order/index',
                                        'name' => '财务管理',
                                        'children' => [[
                                            'dataPermission' => 2,
                                            'icon' => '',
                                            'id' => 3,
                                            'isPageMenu' => 1,
                                            'level' => 3,
                                            'linkType' => 1,
                                            'linkUrl' => '/order/index',
                                            'name' => '财务管理', ]], ], ], ]];
    }

    /**
     * 验证规则.
     *
     * @return array 响应数据
     */
    protected function rules(): array
    {
        return [
            'name' => 'required | string | min:1 | bail',
            'order' => 'required | int | bail',
            'priceRule' => 'required',
        ];
    }

    /**
     * 验证错误提示.
     * @return array 响应数据
     */
    protected function messages(): array
    {
        return [
            'name.required' => '等级名称 必填',
            'name.string' => '等级名称 必需为字符串',
            'name.min' => '等级名称 字符串长度不可小于1',
            'order.required' => '排序 必填',
            'order.integer' => '排序 必需为整数',
            'priceRule.required' => '扣币规则 必填',
        ];
    }
}
